/* Copyright (c) 2010, mitsuhisa.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of the misuhisa.net nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <cppunit/extensions/HelperMacros.h>
#include "../libnyu/Value.hpp"

using namespace nyu;

class ValueTest : public CPPUNIT_NS::TestFixture {
    CPPUNIT_TEST_SUITE(ValueTest);
    CPPUNIT_TEST(testValueSize);
    CPPUNIT_TEST(testValueTag);
    CPPUNIT_TEST(testValueTag2);
    CPPUNIT_TEST(testIsInt);
    CPPUNIT_TEST(testIsObject);
    CPPUNIT_TEST(testIsNull);
    CPPUNIT_TEST(testIntToValue);
    CPPUNIT_TEST(testObjectToValue);
    CPPUNIT_TEST(testValueToInt);
    CPPUNIT_TEST(testValueToObject);
    CPPUNIT_TEST_SUITE_END();

public:
    void testValueSize() {
        CPPUNIT_ASSERT_EQUAL(sizeof(void*), sizeof(Value));
    }

    void testValueTag() {
        Value v;

        v = 0;
        CPPUNIT_ASSERT_EQUAL(value_tag(v), VALUE_TAG_INT);

        v = 1;
        CPPUNIT_ASSERT_EQUAL(value_tag(v), VALUE_TAG_OBJECT);
    }

    void testValueTag2() {
        Value v1, v2;

        v1 = 0;
        v2 = 0;
        CPPUNIT_ASSERT_EQUAL(value_tag(v1, v2), VALUE_TAG_INT_INT);

        v1 = 0;
        v2 = 1;
        CPPUNIT_ASSERT_EQUAL(value_tag(v1, v2), VALUE_TAG_INT_OBJECT);

        v1 = 1;
        v2 = 0;
        CPPUNIT_ASSERT_EQUAL(value_tag(v1, v2), VALUE_TAG_OBJECT_INT);

        v1 = 1;
        v2 = 1;
        CPPUNIT_ASSERT_EQUAL(value_tag(v1, v2), VALUE_TAG_OBJECT_OBJECT);
    }

    void testIsInt() {
        Value v;

        v = 0;
        CPPUNIT_ASSERT(is_int(v));

        v = 1;
        CPPUNIT_ASSERT(!is_int(v));

        v = NULL_VALUE;
        CPPUNIT_ASSERT(!is_int(v));
    }

    void testIsObject() {
        Value v;

        v = 0;
        CPPUNIT_ASSERT(!is_object(v));

        v = 1;
        CPPUNIT_ASSERT(is_object(v));

        v = NULL_VALUE;
        CPPUNIT_ASSERT(is_object(v));
    }

    void testIsNull() {
        Value v;

        v = 0;
        CPPUNIT_ASSERT(!is_null(v));

        v = 1;
        CPPUNIT_ASSERT(is_null(v));

        v = 3;
        CPPUNIT_ASSERT(!is_null(v));

        v = NULL_VALUE;
        CPPUNIT_ASSERT(is_null(v));
    }

    void testIntToValue() {
        CPPUNIT_ASSERT_EQUAL(int_to_value(0), Value(0));
        CPPUNIT_ASSERT_EQUAL(int_to_value(1), Value(2));
        CPPUNIT_ASSERT_EQUAL(int_to_value(-1), Value(-2));
    }

    void testObjectToValue() {
        CPPUNIT_ASSERT_EQUAL(object_to_value(0), Value(1));
        CPPUNIT_ASSERT_EQUAL(object_to_value(reinterpret_cast<Object*>(2)), Value(3));
    }

    void testValueToInt() {
        CPPUNIT_ASSERT_EQUAL(value_to_int(0), int_type(0));
        CPPUNIT_ASSERT_EQUAL(value_to_int(2), int_type(1));
        CPPUNIT_ASSERT_EQUAL(value_to_int(-2), int_type(-1));
    }

    void testValueToObject() {
        CPPUNIT_ASSERT_EQUAL(value_to_object(1), reinterpret_cast<Object*>(0));
        CPPUNIT_ASSERT_EQUAL(value_to_object(3), reinterpret_cast<Object*>(2));

        CPPUNIT_ASSERT_EQUAL(value_to_object(NULL_VALUE), reinterpret_cast<Object*>(0));
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(ValueTest);
